<?php

class AdminMenuService
{
   /*
    * 新增menu
    *
    */
    public  static function addMenu($menu_name,$parent_id,$url)
    {
        $menu = AdminMenu::model()->find("menu_name ='".$menu_name."'");


        //是否存在
        if(empty($menu)==false) {
            if ($menu->is_delete == 0)
                throw new Exception("该菜单已存在");
        }

        //一级菜单是否被删除

        if($parent_id >0)
        {
            $oldmenu = AdminMenu::model()->findByPK($parent_id);

            if($oldmenu->is_delete == 1)
                throw new Exception("一级菜单已被删除");

        }

        //不存在
        if(empty($menu)==true)
        {
            $menu = new AdminMenu();

            Util::initObjData($menu);

        }

        $menu->is_delete = 0;
        $menu->menu_name = $menu_name;
        $menu->parent_id = $parent_id;
        $menu->url = $url;

        $r1= $menu->save();

        if (!$r1) {

             print_r($menu->errors);

            throw new Exception("菜单添加失败");
        }






    }

    /*
    * 删除menu
    *
    */
    public  static function deleteMenu($menu_id)
    {


        $menu = AdminMenu::model()->findByPK($menu_id);


        //是否存在
        if(empty($menu)) {
                throw new Exception("该菜单不存在");
        }

        if($menu->is_delete == 1)
            throw new Exception("该菜单已经被删除");



        $connection=Yii::app()->db;

        $transaction=$connection->beginTransaction();//事物开始

        try
        {
            //删除该菜单

            Util::deleteObjData($menu);
            Util::saveObjData($menu);

            //如果是一级菜单，删除该一级菜单下所有二级菜单
            if($menu->parent_id == -1)
            {
                $slist =AdminMenu::model()->findAll(Util::initSearchSql()." and parent_id = ".$menu_id);

                $count = count($slist);


                if($count >0)
                {
                    for($i=0;$i<$count;$i++)
                    {
                        Util::deleteObjData($slist[$i]);
                        Util::saveObjData($slist[$i]);
                    }


                }

            }

            $transaction->commit();//事物结束
        }catch (Exception $e)
        {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }






    }

    /*
   * 修改menu
   *
   */
    public  static function updateMenu($menu_id,$menu_name,$url)
    {


        $menu = AdminMenu::model()->findByPK($menu_id);


        //是否存在
        if(empty($menu)) {
            throw new Exception("该菜单不存在");
        }

        if($menu->is_delete == 1)
            throw new Exception("该菜单已经被删除");

        if($menu_name == $menu->menu_name)
        {

        }
        else
        {
            $other_menu = AdminMenu::model()->find("menu_name ='".$menu_name."'");

            if(empty($other_menu)==false)
                throw new Exception("该菜单名已存在");


        }


        $menu->menu_name = $menu_name;
        $menu->url = $url;

        Util::updateObjData($menu);
        Util::saveObjData($menu);


    }


    /*
     * 获取菜单列表
     *
     */
    public static function getMenuList($searchsql)
    {
        //获取一级菜单
        $firstlevellist = AdminMenu::model()->findAll($searchsql." and parent_id=-1 order by priority asc,menu_id asc ");

        //初始化返回列表
        $returnlist = array();

        //获取二级菜单
        for($i=0;$i<count($firstlevellist);$i++)
        {
            $pid = $firstlevellist[$i]->menu_id;

            //获取二级列表
            $mlist = AdminMenu::model()->findAll($searchsql." and parent_id=".$pid ." order by priority asc,menu_id asc");

            //插入一级列表
            array_push($returnlist,$firstlevellist[$i]);

            //插入二级列表
            for($t=0;$t<count($mlist);$t++)
                array_push($returnlist,$mlist[$t]);

        }

        return $returnlist;
    }


}


?>